﻿
using System;
using System.Reflection;
using Family.Core.Model;

namespace Family.Tests.Extensions
{
    internal static class EntityIdSetter
    {
        public static void SetIdOf(EntityWithTypeId<Int32> entity, Int32 id)
        {
            // Set the data property reflectively
            PropertyInfo idProperty = entity.GetType().GetProperty("Id", BindingFlags.Public | BindingFlags.Instance);

            if (idProperty == null)
            {
                throw new Exception("id Property could not be found");
            }

            idProperty.SetValue(entity, id, null);
        }

        public static TEntity SetIdTo<TEntity>(this TEntity entity, Int32 id)
            where TEntity : EntityWithTypeId<Int32>
        {
            SetIdOf(entity, id);
            return entity;
        }
    }
}
